﻿using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class TestMultiTouch : MonoBehaviour {

	private Vector3 scale = Vector3.zero;
	private EventSystem eventSystem = null;

	// Use this for initialization
	void Start () {
		eventSystem = EventSystem.current;
	}

	void FixedUpdate() {
		if (Input.touchCount >= 2) {
			EnableEventSystem (false);
		} else {
			EnableEventSystem (true);
			lastDistance = -1f;
		}
	}

	void EnableEventSystem(bool enable) {
		
		if (eventSystem.enabled != enable) {
			eventSystem.enabled = enable;
		}
	}

	private float lastDistance = -1f;

	// Update is called once per frame
	void Update () {
		if (Input.touchCount == 2) {
			Touch touch0 = Input.GetTouch (0);
			Touch touch1 = Input.GetTouch (1);

			Vector2 deltaPos = touch0.position - touch1.position;
			float distance = deltaPos.sqrMagnitude;
			if (lastDistance >= 0) {
				float delta = distance - lastDistance;
				delta *= 0.00001f;
				scale = transform.localScale;
				scale.x += delta;
				if (scale.x < 0.5f) {
					scale.x = 0.5f;
				} else if (scale.x > 2) {
					scale.x = 2;
				}
				scale.y = scale.x;
				transform.localScale = scale;
			}
			lastDistance = distance;
		}
	}
}
